#!/usr/bin/perl
use boolean;
use Switch;

#body part
use enum qw(
    NONE
    IGNORE
    SCENARIO_NO
    SCENARIO
    CATEGORY
    GIVEN
    WHEN
    THEN
    DOCEND);

my($LineType) = NONE;
my($StartDoc) = false;

open( IN, "in.erl");
open( OUT, ">out.erl");

my @data = <IN>;

foreach my $line (@data)
{
    # check to start parsing and end parsing
    if($line =~ /\@doc/)
    {
        $StartDoc = true;
        print OUT $line;
        next;
    }

    if($line =~ /DocEnd/)
    {
        $StartDoc = false;
        next;
    }

    if($StartDoc == false)
    {
        print OUT $line;
        next;
    }

    # start parsing body
    if($line =~ /%[\s]*scenario-no/)
    {
        $LineType = SCENARIO_NO;
    }
    elsif($line =~ /%[\s]*scenario/)
    {
        $LineType = SCENARIO;
    }
    elsif($line =~ /%[\s]*category/ )
    {
        $LineType = CATEGORY;
    }

    elsif( $line =~ /%[=\s]*given/)
    {
        $LineType = GIVEN;
        next;
    }
    elsif( $line =~ /%[=\s]*when/)
    {
        $LineType = WHEN;
        next;
    }
    elsif($line =~ /%[=\s]*then/)
    {
        $LineType = THEN;
        next;
    }
    elsif( ( $line =~ /%[\s]*=[\s]*/) && ( $line !~ /%[a-z]/ )
    ||
    ($line =~ /^$/)
    )
    {
        next;
    }

    # replace & writing ouput
    switch($LineType)
    {
        # parsing inline
        case(SCENARIO_NO){
            $line =~ s/%[\s]*scenario-no/%\% target-scenario-no/;
        }
        case(SCENARIO){
            $line =~ s/%[\s]*scenario/%\% target-scenario/;
        }
        case(CATEGORY){
            $line =~ s/%[\s]*category/%\% target-category/;
        }

        # parsing segment
        case(GIVEN){
           $line =~ s/%/%\% target-given /;
        }
        case(WHEN) {
            $line =~ s/%/%\% target-when /;
        }
        case(THEN){
           $line =~ s/%/%\% target-then /;
        }
    }

    print OUT $line;
}

close(IN);
close(OUT);
print "successfully";
